package com.jhanlos.flights.objects;

import gov.nasa.worldwind.geom.Angle;
import gov.nasa.worldwind.geom.Vec4;
import gov.nasa.worldwind.globes.Globe;

import java.io.Serializable;

public class Flights implements Serializable {

	
	Airport[] airport;
	 int iter=0;
	double[][] flights;
	Vec4 fromAir,toAir,mediun;
	public Flights(Globe globe) {
		
		double []from = {85*Math.PI/180.0, -71.533333*Math.PI/180.0,2400};
        //double []to = {24.142222*Math.PI/180.0, -110.3*Math.PI/180.0,500};
        double []to = {-85*Math.PI/180.0, -71*Math.PI/180.0,500};
		airport = new Airport[2];
		
		airport[0] = new Airport("Arequipa",from[0],from[1],from[2]);
		
		airport[1] = new Airport("Miami",to[0],to[1],to[2]);
		
		fromAir = globe.computePointFromPosition(
				Angle.fromRadiansLatitude(from[0]), 
                    Angle.fromRadiansLongitude(from[1]),from[2]);
		toAir = globe.computePointFromPosition(
				Angle.fromRadiansLatitude(to[0]), 
                Angle.fromRadiansLongitude(to[1]),to[2]);
		
		
		mediun = new Vec4((toAir.x + fromAir.x)/2.0,(toAir.y + fromAir.y)/2.0,(toAir.z + fromAir.z)/2.0);
		
		double dist = Math.sqrt(Math.pow(toAir.x - fromAir.x,2.0)+ Math.pow(toAir.y - fromAir.y,2.0)+Math.pow(toAir.z - fromAir.z,2.0));
		
		double radiusAll = globe.getEquatorialRadius()+ (from[2]>to[2]?from[2]:to[2]);
		double tangMedio = 	Math.sqrt((1 -mediun.getLength3()/radiusAll)/2 /(1 +mediun.getLength3()/radiusAll)/2 );	
		double pendi = (dist/2)*((1 + tangMedio)/(1 + tangMedio))- radiusAll + mediun.getLength3();
		
		 
		pendi/=8; 
		
        ////////////////////////////////////////
        iter = 200;
        ////////////////////////////////////////
        
        
        flights = new double[2*iter+1][3];
       
        double delta;
        Vec4 temp;// = new Vec4(fromAir.x,fromAir.y,fromAir.z);
        int i=0;
        
        for ( i= 0; i < iter; i++) {
        	//Math.sqrt(iter*iter-(i-iter)*(i-iter))*pendi/(iter);
        	delta = Math.sqrt(iter*iter-(i-iter)*(i-iter))*pendi/(iter);//Math.sqrt(pendi*pendi*i/iter);//i*pendi/iter;//Math.sqrt(pendi*pendi*i/iter);
        	temp = new Vec4((toAir.x - fromAir.x)*i/(2*iter) + fromAir.x ,(toAir.y-fromAir.y)*i/(2*iter) + fromAir.y,(toAir.z-fromAir.z)*i/(2*iter) + fromAir.z);
        	
        	double value = temp.getLength3();
        	flights[i][0] = (radiusAll + delta)*temp.x/value;
        	flights[i][1] = (radiusAll + delta)*temp.y/value;
        	flights[i][2] = (radiusAll + delta)*temp.z/value;
        	
        }
         
        for ( ; i < iter*2; i++) {
        	//Math.sqrt(iter*iter-(i-iter)*(i-iter))*pendi/(iter);
        	delta =Math.sqrt(iter*iter-((2*iter-i)-iter)*((2*iter-i)-iter))*pendi/(iter);  //Math.sqrt(pendi*pendi*(2*iter-i)/iter);//(2*iter-i)*pendi/iter;//Math.sqrt(pendi*pendi*i/iter);
        	temp = new Vec4((toAir.x - fromAir.x)*i/(2*iter) + fromAir.x ,(toAir.y-fromAir.y)*i/(2*iter) + fromAir.y,(toAir.z-fromAir.z)*i/(2*iter) + fromAir.z);
        	
        	double value = temp.getLength3();
        	flights[i][0] = (radiusAll + delta)*temp.x/value;
        	flights[i][1] = (radiusAll + delta)*temp.y/value;
        	flights[i][2] = (radiusAll + delta)*temp.z/value;


        
        }
        
        flights[iter*2][0] = toAir.x;
    	flights[iter*2][1] = toAir.y;
    	flights[iter*2][2] = toAir.z;
		
	}
	
	
	public int getNumFlights() {
		
		return iter*2+1;
	}

	public double[] getFlight(int i) {
		
		return new double[] {flights[i][0],flights[i][1],flights[i][2]};
	}

	public Airport[] getAirports() {
		return airport;
	}
}
